﻿package 
{

	import flash.display.MovieClip;
	import connection.Connection;
	import flash.utils.ByteArray;
	import flash.events.ProgressEvent;
	import com.hurlant.math.BigInteger;
	import encrypt.BigIntFactory;
	import encrypt.RSA;
	import connection.Packet;
	import HandlerEvent.PacketHandler;
	import flash.utils.Endian;
	import flash.events.IOErrorEvent;


	public class StartSession extends MovieClip
	{

		private var conn:Connection;
		private var nHexStr:String;
		private var eHexStr:String;
		private var encryptKey:ByteArray;
		private var host:String;
		private var port:int;
		public var par;
		private var loginForm:LoginForm;

		public function StartSession()
		{
			// constructor code
		}

		public function doListener(... arg):void
		{
			conn.addEventListener(PacketHandler.RECEIVE_NORMAL_PASSIVE_PACKET,doStartSession);
			conn.addEventListener(PacketHandler.RECEIVE_NORMAL_ACTIVE_PACKET,doSendLogin);
			conn.addEventListener(IOErrorEvent.IO_ERROR,doErrorIO);
		}

		public function doErrorIO(event:IOErrorEvent):void{
			this.par.txtAlert.text = "Không thể kết nối đến server. Mời bạn thử lại sau!";
		}
								  
		
		public function doSendLogin(event:PacketHandler):void
		{
			if (conn.getReceivePacket().getCommandCode() == "CRT" && conn.getReceivePacket().getCommandType() == 1)
			{
				var okConnect:int = conn.getReceivePacket().getContent().indexOf("ok");
				if ((okConnect != -1))
				{
					loginForm = new LoginForm  ;
					loginForm.x = 250;
					loginForm.y = 100;
					loginForm.parentFrame = this.par;
					loginForm.setConnection(conn);
					loginForm.doAction();
					par.addChild(loginForm);
				}
			}
		}

		public function doStartSession(event:PacketHandler):void
		{
			if (conn.getReceivePacket().getCommandCode() == "CRT" && conn.getReceivePacket().getCommandType() == 0)
			{
				var jsonParse = JSON.parse(conn.getReceivePacket().getContent());
				nHexStr = jsonParse[0];
				eHexStr = jsonParse[1];

				var n:BigInteger = BigIntFactory.Create(nHexStr);
				var e:BigInteger = BigIntFactory.Create(eHexStr);
				var d:BigInteger = BigIntFactory.Create("0x456ed46eb93a9f9a9607f9b932ecfc84c8dc25a77abafc01");
				var rsa:RSA = new RSA(e,d,n);

				var packetSend:Packet = new Packet  ;
				packetSend.setHeaderFlag(0);
				packetSend.setCommandCode("CRT");
				packetSend.setCommandType(1);
				var randomValue:int = Math.floor(Math.random() * 1000000000);
				packetSend.setCommandSenderID(randomValue);

				var myDate:Date = new Date  ;
				var myDateStr:String = myDate.toString();
				var byteArrayDate:ByteArray = new ByteArray  ;
				byteArrayDate.endian = Endian.LITTLE_ENDIAN;
				byteArrayDate.writeUTFBytes(myDateStr);
				encryptKey = byteArrayDate;
				var keySend:String = "0x";
				keySend +=  rsa.encrypt(byteArrayDate);
				packetSend.setContent(JSON.stringify([keySend]));
				packetSend.setHeaderDataLength(8 + packetSend.getContent().toString().length);
				packetSend.printPacket();
				conn.sendPacket(packetSend);
				trace("Gui goi tin chua key");
			}
		}

		public function setConnection(c:Connection):void
		{
			conn = c;
		}

		public function getNHexStr():String
		{
			return this.nHexStr;
		}

		public function getEHexStr():String
		{
			return this.eHexStr;
		}

		public function getEncryptKey():ByteArray
		{
			return this.encryptKey;
		}


	}

}